package com.qisu.cacheMap;

import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;


/**
 * 分时电量增长量缓存
 * @author li
 *
 */
@Component
@Configuration
public class ChargeQuantity {
	private Log log = LogFactory.get();
	private ConcurrentHashMap<String, Integer> electricityCache = new ConcurrentHashMap<>();
	private ObjectMapper mapper = new ObjectMapper();

	@Value("${logging.feature.channelList:false}") private boolean enable;
	
	public ChargeQuantity put(String key, Integer value) {
		electricityCache.put(key, value);
		
		if(enable) {
			log.debug("key: " + key);
			
			try {
				log.debug("cache: " + mapper.writeValueAsString(electricityCache));
			} catch (JsonProcessingException e) {
				e.printStackTrace();
			}
		}

		return this;
	}

	
	public Integer get(String key) {
		if(electricityCache.containsKey(key)) {
			return electricityCache.get(key);
		}
		return null;
	}

	public void remove(String key) {
		if(electricityCache.containsKey(key)) {
			electricityCache.remove(key);
			log.debug(key + " removed from ELECTRICITYCACHE");
		}
	}


	public int size() {
		return electricityCache.size();
	}
}
